var url="/tbpoject/store/"
var pc=1;
$(function(){
	getAllStore(pc);
})

//查询所有的店铺信息
function getAllStore(pc){
	var tbody=$("#tbody")
	$.getJSON(url+"all",{"pageCode":pc},function(data){
		tbody.html("");
		$(".pagination").html("");
		for(var i=0;i<data.beanList.length;i++){
			tbody.append("<tr>"
						+"<td>"+data.beanList[i].id+"</td>"
						+"<td>"+data.beanList[i].name+"</td>"
						+"<td>"+data.beanList[i].info.name+"</td>"
						+"<td>"+data.beanList[i].info.sex+"</td>"
						+"<td>"+data.beanList[i].info.birthday+"</td>"
						+"<td>"+data.beanList[i].info.number+"</td>"
						+"<td>"+data.beanList[i].level+"</td>"
						+"<td>"+data.beanList[i].info.time+"</td>"
						+"<td style='display:none;'>"+data.beanList[i].uid+"</td>"//商家id
						+"<td>"
						+'<a href="javascript:updateStoreT('+data.beanList[i].id+','+data.pc+')">编辑</a>&nbsp;&nbsp;&nbsp;'
						+'<a href="javascript:deleteS('+data.beanList[i].id+','+data.pc+')">删除</a>'
						+"</tr>")
		} 
		$(".pagination").html(getPage(data,"getAllStore"));
	})
}

//根据姓名查询店铺
function getStoreByName(){
	var name=$("#sname").val();
	/*alert(name)*/
	$.post(url+"queryStoreByName",{"name":name},function(data){
		if(data=="error"){
			var tbody=$("#tbody");
			tbody.html("");
			
			$(".pagination").html("");
			tbody.append("<tr><td>数据为空!!</td></tr>");
		}else{
			var item=$.parseJSON(data);
			var tbody=$("#tbody");
			tbody.html("");
			$(".pagination").html("");
		for(var i=0;i<item.length;i++){
			tbody.append("<tr>"
					+"<td>"+item[i].id+"</td>"
					+"<td>"+item[i].name+"</td>"
					+"<td>"+item[i].info.name+"</td>"
					+"<td>"+item[i].info.sex+"</td>"
					+"<td>"+item[i].info.birthday+"</td>"
					+"<td>"+item[i].info.number+"</td>"
					+"<td>"+item[i].level+"</td>"
					+"<td>"+item[i].info.time+"</td>"
					+"<td style='display:none;'>"+item[i].uid+"</td>"//商家id
					+"<td>"
					+'<a href="javascript:updateStoreP('+item[i].id+')">编辑</a>&nbsp;&nbsp;&nbsp;'
					+'<a href="javascript:delStore('+item[i].id+')">删除</a>'
					+"</tr>")
		} 
		}
	});
}
//分页修改跳转
function updateStoreT(id,pc){
	window.location=url+"updateStoreT?id="+id+"&pc="+pc ;
}
//通过查询姓名
function updateStoreT(id){
	window.location=url+"updateStoreT?id="+id;
}
//分页删除店铺
var uid;
var upIndex;
function deleteS(id,pageIndex){
	uid=id;
	upIndex=pageIndex;
	swal({
		title: "确定要删除吗？",
		text: "删除后，数据不能恢复",
		type: "warning",
		showCancelButton: true,
		cancelButtonText:'取消',
		confirmButtonColor: '#DD6B55',
		confirmButtonText: '确定',
		closeOnConfirm: false
	},
	function(isConfirm){
		if(isConfirm){
			deleteStore();
		}
			
	});
}
//删除商铺
function deleteStore(){
	var id=uid
	pc=upIndex
	$.post(url+"deleteStore",{"id":id},function(data){
		if(data=="success"){
			swal("删除成功!", "店铺信息已删除", "success");
			getAllStore(pc)
		}else{
			swal("删除失败!", "店铺信息删除失败", "error");
		}
	})
}